package cn.gailvlun.gll.presentation.login;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;

import com.github.markzhai.recyclerview.BaseViewAdapter;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import cn.gailvlun.gll.R;
import cn.gailvlun.gll.databinding.ActivitySelectCountryCodeBinding;
import cn.gailvlun.gll.presentation.base.BaseActivity;
import cn.gailvlun.gll.presentation.login.adapter.SelectCountryCodeAdapter;
import cn.gailvlun.gll.presentation.login.bean.CountryCode;
import cn.gailvlun.gll.util.SPUtil;
import cn.gailvlun.gll.widget.LetterIndexView;
import cn.gailvlun.gll.widget.NavigationBarView;
import io.reactivex.Observable;
import rx_activity_result2.Result;
import rx_activity_result2.RxActivityResult;



public class SelectCountryCodeActivity extends BaseActivity {

    public static Observable<Result<Activity>> openActivity(Activity context, String countryCode) {
        Intent intent = new Intent(context, SelectCountryCodeActivity.class);
        intent.putExtra("countryCode", countryCode);
        return RxActivityResult.on(context)
                .startIntent(intent);
    }

    private ActivitySelectCountryCodeBinding mBinding;
    private SelectCountryCodeAdapter mAdapter;
    private LinearLayoutManager mLayoutManager;

    @Override
    protected void initComponent() {
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_select_country_code);

        mAdapter = new SelectCountryCodeAdapter(mContext);
        mAdapter.setPresenter(new Presenter());
        mBinding.rv.setAdapter(mAdapter);

        mLayoutManager = new LinearLayoutManager(mContext);
        mBinding.rv.setLayoutManager(mLayoutManager);
    }

    @Override
    protected void createHandler() {
        mBinding.nbv.setOnBackListener(new NavigationBarView.OnBackListener() {
            @Override
            public void onBack(NavigationBarView nb) {
                onBackPressed();
            }
        });
        mBinding.liv.setOnTouchLetterListener(new LetterIndexView.OnTouchLetterListener() {
            @Override
            public void onTouchLetter(char letter) {
                int position = mAdapter.getFirstLetterPosition(letter);
                if (position != -1) {
                    mLayoutManager.scrollToPositionWithOffset(position, 0);
                }
            }
        });
    }

    @Override
    protected void loadData(@Nullable Bundle savedInstanceState) {
        String[] countryCodes = getResources().getStringArray(R.array.country_code);

        List<CountryCode> countryCodeList = new ArrayList<>();
        for (String countryCode : countryCodes) {
            countryCodeList.add(CountryCode.transform(countryCode));
        }
        Collections.sort(countryCodeList);

        char lastFirstLetter = 'a';
        for (CountryCode countryCode : countryCodeList) {
            char thisFirstLetter = countryCode.getFirstLetter();
            if (lastFirstLetter != thisFirstLetter) {
                mAdapter.add(String.valueOf(thisFirstLetter), SelectCountryCodeAdapter.Type.HEADER.ordinal());
                lastFirstLetter = thisFirstLetter;
            }
            mAdapter.add(countryCode, SelectCountryCodeAdapter.Type.COUNTRY.ordinal());
        }

        String countryCode = getIntent().getStringExtra("countryCode");
        if (countryCode != null) {
            int pos = mAdapter.getCountryCodePosition(countryCode);
            if (pos != -1) {
                mLayoutManager.scrollToPositionWithOffset(pos, 0);
            }
        }
    }

    public class Presenter implements BaseViewAdapter.Presenter {
        public void onSelectCountryCode(CountryCode item) {
            SPUtil.put(SPUtil.GLOBAL, "countryCode", item);

            Intent intent = new Intent();
            intent.putExtra("countryCode", item);
            setResult(RESULT_OK, intent);
            finish();
        }
    }
}
